package com.henrybit.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.henrybit.entity.HotelInfo;
import com.henrybit.exception.DBException;
import com.henrybit.util.Constant;
import com.henrybit.util.DBFactory;

/**
 * 酒店信息DB操作类<p>
 * @author henrybit
 * @version 1.0
 */
public class HotelInfoDao extends BaseDao<HotelInfo> {
	/**查询基础SQL*/
	private static String queryPre = "select * from hotel_info where 1=1";
	/**分页默认大小*/
	private static int pageSize = 6;
	/**分页编号*/
	private static int pageNo = 1;
	
	@Override
	public List<HotelInfo> query(HotelInfo t) throws DBException {
		// TODO Auto-generated method stub
		return query(t, 0);
	}
	
	/**
	 * 返回酒店信息列表<p>
	 * @param t
	 * @param page
	 * @return
	 * @throws DBException
	 */
	public List<HotelInfo> query(HotelInfo t, int page) throws DBException {
		List<HotelInfo> list = new ArrayList<HotelInfo>();
		String sql = prepareQuerySql(t);
		Connection con = DBFactory.getConnection();
		PreparedStatement ps = null;
		try {
			con.setAutoCommit(false);
			ps = con.prepareStatement(sql);
			ResultSet rs = ps.getResultSet();
			while(rs.next()) {
				HotelInfo hotelInfo = getHotelInfo(rs);
				list.add(hotelInfo);
			}
			rs.close();
			ps.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new DBException(e, Constant.DB_QUERY_HOTEL_INFO_ERROR, "准备酒店查询SQL执行异常");
		} finally {
			try {
				con.close();
			} catch(SQLException e) {
				throw new DBException(e, Constant.DB_CONNECTION_CLOSE_ERROR, "关闭数据库连接异常");
			}
		}
		return list;
	}
	
	public List<HotelInfo> query(HotelInfo t, int page, int pageSize) throws DBException {
		
		return null;
	}

	@Override
	public void insert(HotelInfo t) throws DBException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean update(HotelInfo t) throws DBException {
		// TODO Auto-generated method stub
		return false;
	}
	
	private String prepareQuerySql(HotelInfo t) {
		int id = t.getId();
		String name = t.getName();
		StringBuilder sql = new StringBuilder(queryPre);
		if(id > 0) {
			sql.append(" and id=").append(id);
		}
		if(name != null && !name.equals("")) {
			sql.append(" and name like '%").append(name).append("%'");
		}
		return sql.toString();
	}
	
	private String prepareInsertSql(HotelInfo t) {
		
		return null;
	}
	
	private String prepareUpdateSql(HotelInfo t) {
		
		return null;
	}
	
	/**
	 * 封装酒店信息<p>
	 * @param rs
	 * @return
	 * @throws DBException
	 */
	private HotelInfo getHotelInfo(ResultSet rs) throws DBException {
		HotelInfo hotelInfo = new HotelInfo();
		try {
			int id = rs.getInt("id");
			String name = rs.getString("name");
			String englishName = rs.getString("englishName");
			int level = rs.getInt("level");
			String url = rs.getString("url");
			int houseNum = rs.getInt("houseNum");
			String hotelDevice = rs.getString("hotelDevice");
			String hotelDescription = rs.getString("hotelDescription");
			String houseDescription = rs.getString("houseDescription");
			String hotelImages = rs.getString("hotelImages");
			String houseImages = rs.getString("houseImages");
			String suggestionIdea = rs.getString("suggestionIdea");
			int landId = rs.getInt("landId");
			hotelInfo.setId(id);
			hotelInfo.setName(name);
			hotelInfo.setEnglishName(englishName);
			hotelInfo.setLevel(level);
			hotelInfo.setUrl(url);
			hotelInfo.setHouseNum(houseNum);
			hotelInfo.setHotelDevice(hotelDevice);
			hotelInfo.setHotelDescription(hotelDescription);
			hotelInfo.setHouseDescription(houseDescription);
			hotelInfo.setHotelImages(hotelImages);
			hotelInfo.setHouseImages(houseImages);
			hotelInfo.setSuggestionIdea(suggestionIdea);
			hotelInfo.setLandId(landId);
		} catch (Exception e) {
			throw new DBException(e, Constant.DB_HOTELINFO_PREPARE_DATA_ERROR, "封装酒店信息类异常");
		}
		return hotelInfo;
	}
}
